package com.lune.job.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lune.job.framework.redis.RedisUtil;
import com.lune.job.pojo.Post;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.service.IPostService;
import com.lune.job.service.IUserDataService;
import com.lune.job.utils.UserThreadLocal;
import com.lune.job.vo.UserData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/post")
@Api(tags = "公司的职位管理")
public class PostController {

    @Autowired
    private IPostService postService;

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IUserDataService userDataService;

    @PostMapping("/")
    @ApiOperation(value = "添加职位信息")
    public RespBean create(@RequestBody Post post) {
        String token = UserThreadLocal.get();
        UserData userData = (UserData) redisUtil.get(token);
        post.setCompany_id(userData.getId());
        boolean flag = postService.save(post);
        if (flag) {
            return RespBean.success("添加职位信息成功");
        } else {
            return RespBean.error("添加职位信息失败");
        }

    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除职位")
    public RespBean delete(@PathVariable Integer id) {
        if (postService.removeById(id)) {
            return RespBean.success("删除职位成功");
        } else {
            return RespBean.error("删除职位失败");
        }
    }

    @DeleteMapping("/")
    @ApiOperation(value = "批量删除职位")
    public RespBean deleteByIds(Integer[] ids) {
        if (postService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("批量删除职位成功");
        } else {
            return RespBean.error("批量删除职位失败");
        }
    }

    @PutMapping("/")
    @ApiOperation(value = "修改职位")
    public RespBean update(@RequestBody Post post) {
        if (postService.updateById(post)) {
            return RespBean.success("修改职位成功");
        } else {
            return RespBean.error("修改职位失败");
        }
    }

    @GetMapping("/detail")
    @ApiOperation(value = "根据id查询职位信息")
    public RespBean detail(Integer id) {
        Post byId = postService.getById(id);
        if (byId != null) {
            return RespBean.success("查询成功", byId);
        } else {
            return RespBean.error("查询失败");
        }
    }

    @GetMapping("/queryPage")
    @ApiOperation(value = "分页查询职位信息")
    public ResPageBean queryPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                 @RequestParam(defaultValue = "10") Integer size,
                                 Post post) {
        UserData userData = userDataService.getUserData();
        post.setCompany_id(userData.getId());
        return postService.queryPage(currentPage, size, post);
    }

    @PostMapping("/queryByCon")
    @ApiOperation(value = "条件查询职位信息")
    public List<Post> queryByCon(@RequestBody Post post) {
        UserData userData = userDataService.getUserData();
        post.setCompany_id(userData.getId());
        return postService.queryByCon(post);
    }

    @GetMapping("/count")
    @ApiOperation(value ="根据公司id获取职位的数量" )
    public int count(){
        QueryWrapper<Post> postQueryWrapper = new QueryWrapper<>();
        UserData userData = userDataService.getUserData();
        postQueryWrapper.eq("company_id",userData.getId());
        return postService.count(postQueryWrapper);
    }




}
